#include "mainwindow.h"
#include <QVBoxLayout>
#include <QCoreApplication>
#include <QMessageBox>
#include <QDebug>
#include "globalobject.h"

MainWindow::MainWindow(QWidget *parent)
    : basewindow(parent)
{
    gmainwid = this;
    setMinimumSize(1000,650);
    connectDatabase();
    initControl();
    setWidegetLayout();

}

MainWindow::~MainWindow()
{

}

bool MainWindow::connectDatabase()
{
    QString strpath = QCoreApplication::applicationDirPath();
    strpath += "/datainfo.db";
    qDebug()<< strpath;
    m_db = QSqlDatabase::addDatabase("QSQLITE");
    m_db.setDatabaseName(strpath);
    if(!m_db.open())
    {
        QMessageBox::critical(0, QObject::tr("Database Error"),
        m_db.lastError().text());
        return false;
    }
    return true;
}

bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(eventType);
    MSG* msg = reinterpret_cast<MSG*>(message);
    return winEvent(msg, result);
}

bool MainWindow::winEvent(MSG *message, long *result)
{
    switch(message->message)
    {
        case WM_NCHITTEST:
            int xPos = GET_X_LPARAM(message->lParam) - this->frameGeometry().x();
            int yPos = GET_Y_LPARAM(message->lParam) - this->frameGeometry().y();
            //(使用nativeEvent时必须进行判断，不然其他控件会实效！！！)

            //判断鼠标是否位于控件上
            if(this->childAt(xPos,yPos) != m_mainwid
               &&this->childAt(xPos,yPos) != m_topwid
               &&this->childAt(xPos,yPos) != m_contentwid)
            {
                return false;
            }

            //鼠标区域位于窗体边框，进行缩放
            if(xPos > 0 && xPos < m_border)
                *result = HTLEFT;
            if(xPos > (this->width() - m_border) && xPos < (this->width()))
                *result = HTRIGHT;
            if(yPos > 0 && yPos < m_border)
                *result = HTTOP;
            if(yPos > (this->height() - m_border) && yPos < (this->height()))
                *result = HTBOTTOM;
            if(xPos > 0 && xPos < m_border && yPos > 0 && yPos < m_border)
                *result = HTTOPLEFT;
            if(xPos > (this->width() - m_border) && xPos < (this->width()) && yPos > 0 && yPos < m_border)
                *result = HTTOPRIGHT;
            if(xPos > 0 && xPos < m_border && yPos > (this->height() - m_border) && yPos < (this->height()))
                *result = HTBOTTOMLEFT;
            if(xPos > (this->width() - m_border) && xPos < (this->width()) && yPos > (this->height() - m_border) && yPos < (this->height()))
                *result = HTBOTTOMRIGHT;

            //鼠标区域位于标题内部，进行移动
            int border = m_border+1;
            if(QRect(border,border,this->width()-border*2,91).contains(xPos,yPos))
            {
                *result = HTCAPTION;
            }

            return true;
    }
    return false;
}

void MainWindow::initControl()
{
    m_topwid = new topwidget(this);
    m_contentwid = new contentwidget(this);
    connect(m_topwid,SIGNAL(setCurrentIndex(int)),m_contentwid->m_stackwidget,SLOT(setCurrentIndex(int)));
}

void MainWindow::setWidegetLayout()
{
    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->setContentsMargins(0,0,0,0);
    mainlayout->setSpacing(0);
    mainlayout->addWidget(m_topwid);
    mainlayout->addWidget(m_contentwid);
    m_mainwid->setLayout(mainlayout);
}
